package com.dysoft.flesh.ui;

import com.dysoft.bones.*;
import com.dysoft.flesh.util.QuadTree;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Sean Micklethwaite
 *         Oct 18, 2010 10:32:35 PM
 */
public class UiWidget extends UiComponent implements UiRenderer {

	protected final List<UiRenderer> renderables = new ArrayList<UiRenderer>();
	protected final Property.Delegate<Shape> shape;
	protected final Property.Delegate<Shape> layout;
	protected QuadTree<UiWidget>.Node.Entry entry = null;
	protected Trigger.Dependent monitor = null;
	protected Container container = null;

	public UiWidget(Property<Shape> shape) {
		layout = new Property.Delegate<Shape>(shape);
		this.shape = new Property.CachedDelegate<Shape>(layout);
	}

	public UiWidget(Property.Delegate<Shape> shape) {
		layout = shape;
		this.shape = new Property.CachedDelegate<Shape>(shape);
	}

	public void render(Context ctx) throws DataManager.DataObject.NotPresentException {
		for(UiRenderer renderer : renderables) {
			renderer.render(ctx);
		}
	}

	public Shape getShape() {
		return shape.get();
	}

	public Property<Shape> getLayout() {
		return layout;
	}

	public void setLayout(Property<Shape> shape) {
		layout.setDelegate(shape);
	}

	public void addRenderer(UiRenderer renderer) {
		renderables.add(renderer);
	}

	public void removeRenderer(UiRenderer renderer) {
		renderables.remove(renderer);
	}

	protected void onAdd(Container container) {
		assert this.container == null;
		this.container = container;
	}

	public void remove() {
		assert container != null;
		container.removeWidget(this);
		container = null;
	}

	public Container getContainer() {
		return container;
	}

	public static interface Dynamic {}
	public static interface Container {

		UiWidget addWidget(UiWidget widget);
		void repaint();

		UiWidget addDynamic(UiWidget widget);

		boolean removeDynamic(UiWidget widget);

		void removeWidget(UiWidget widget);
	}
}
